<div class="layui-form-item">
    <button class="layui-btn fabu" style="float:right;margin-left: 10px;margin-right: 10px;">发布分类</button>
    <button class="layui-btn add_" style="float:right;margin-left: 10px;margin-right: 10px;">新建分类</button>
</div>
<br>

<?php if(!empty($type['row'])){

    foreach($type['row'] as $k=>$v){
?>
        <hr class="layui-bg-black">
    <form class="layui-form" action="">
        <input type="hidden" name="" value="<?=$v['id']?>" class="id<?=$k+1?>">
        <div class="layui-form-item">
            <label class="layui-form-label">标签名称</label>
            <div class="layui-input-block" style="width: 300px;">
                <select name="city" lay-verify="required" class="test_biaoqian<?=$k+1?>">
                    <?php foreach($v['select'] as $kk=>$vv){ ?>
                        <?php if($v['test_biaoqian']==$vv['id']){ ?>
                        <option value="<?=$vv['id']?>" selected><?=$vv['name']?></option>
                        <?php }else{ ?>
                            <option value="<?=$vv['id']?>"><?=$vv['name']?></option>
                        <?php } ?>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="<?=$v['test_name']?>" placeholder="请输入分类名称" class="layui-input test_name<?=$k+1?>" style="width: 350px;">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类排序</label>
            <div class="layui-input-block">
                <?php if($v['test_sort']!=999999999){ ?>
                    <input type="text" name="title" value="<?=$v['test_sort']?>" placeholder="请输入分类排序" class="layui-input test_sort<?=$k+1?>" style="width:150px;">
                <?php }else{ ?>
                    <input type="text" name="title" value="0" placeholder="请输入分类排序" class="layui-input test_sort<?=$k+1?>" style="width:150px;">
                <?php } ?>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类操作</label>
            <div class="layui-input-block">
                <?php if($v['test_online']==1){ ?>
                    <button type="button" class="layui-btn layui-btn-normal up_button<?=$k+1?>">上线</button>
                <?php }else if($v['test_online']==2){ ?>
                    <button type="button" class="layui-btn layui-btn-danger down_button<?=$k+1?>">下线</button>
                <?php } ?>
               <button type="button" class="layui-btn save_button<?=$k+1?>">保存</button>
            </div>
        </div>
    </form>
<?php }} ?>

<script>
    //定义一个100的jquery数组
    var arr=[];
    for(var i=1;i<100;i++){
        arr.push(i)
    }
    $.each(arr,function(i,n){
        //已添加分类，上线
        $('.up_button'+n).click(function(){
            var id=$('.id'+n).val();
            $.ajax({
                type:"post",
                data:{id:id},
                dataType:"json",
                url:"<?=base_url("word/type/up_button")?>",
                success:function(res){
                    if(res.code==0){
                        layer.msg(res.msg,{time:1000},function(){
                            window.location.reload()
                        })
                    }else{
                        layer.msg(res.msg,{time:2000})
                    }
                }            })
        })

        //已添加分类，下线
        $('.down_button'+n).click(function(){
            var id=$('.id'+n).val();
            $.ajax({
                type:"post",
                data:{id:id},
                dataType:"json",
                url:"<?=base_url("word/type/down_button")?>",
                success:function(res){
                    if(res.code==0){
                        layer.msg(res.msg,{time:1000},function(){
                            window.location.reload()
                        })
                    }else{
                        layer.msg(res.msg,{time:2000})
                    }
                }
            })
        })

        //已添加分类，保存
        $('.save_button'+n).click(function(){
            var test_name=$('.test_name'+n).val();
            var test_sort=$('.test_sort'+n).val();
            var test_biaoqian=$('.test_biaoqian'+n).val();
            var id=$('.id'+n).val();
            var a=[test_name,test_sort,test_biaoqian]
            $.ajax({
                type:"post",
                data:{test_name:test_name,test_sort:test_sort,test_biaoqian:test_biaoqian,id:id},
                dataType:"json",
                url:"<?=base_url("word/type/save_button")?>",
                success:function(res){
                    if(res.code==0){
                        layer.msg(res.msg,{time:1000},function(){
                            window.location.reload()
                        })
                    }else{
                        layer.msg(res.msg,{time:2000})
                    }
                }
            })
        })

    })

    //发布分类
    $('.fabu').click(function(){
        var fa="not_test";
        $.ajax({
            type:"post",
            data:{fa:fa},
            dataType:"json",
            url:"<?=base_url("word/type/fabu")?>",
            success:function(res){
                if(res.code==0){
                    layer.msg(res.msg,{time:1000},function(){
                        window.location.reload()
                    })
                }else{
                    layer.msg(res.msg,{time:2000})
                }
            }
        })
    })

    //新增分类，把隐藏的表单显示出来
    $(".add_").click(function(){
        //加载弹出层
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.open({
                title:"新增分类",
                type: 1,
                area:['500px','350px'],
                content: content_add_type
            });
        });

        //获取标签
        $.ajax({
            type: "get",
            data: {},
            dataType: "json",
            url: "<?=base_url("word/type/biaoqian")?>",
            success: function (res) {
                if(res.code==0){
//                var arr = JSON.parse(res.data);
                    var arr = res.data;
                    $.each(arr, function (i, n) {
                        $('.hide_city').append("<option value='" + n.id + "'>" + n.name + "</option>");
                    });
                    layui.form.render('select');
                }else{
                    layer.msg(res.msg, {time: 2000});
                }
            }
        })

        //新增分类保存
        $(".hide_save").click(function(){
            var hide_name=$('.hide_name').val();
            var hide_city=$('.hide_city').val();
            var hide_password=$('.hide_password').val();
            var a=[hide_name,hide_city,hide_password];
            $.ajax({
                type: "post",
                data: {hide_name:hide_name,hide_city:hide_city,hide_password:hide_password},
                dataType: "json",
                url: "<?=base_url("word/type/save_type")?>",
                success: function (res) {
                    if(res.code==0){
                        layer.msg(res.msg, {time: 2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.msg(res.msg, {time: 2000});
                    }
                }
            })
        })

        //layui重新渲染下拉框
        layui.form.render('select');

    })

    //新增分类保存的弹出层
    var content_add_type= "<br/><form class=\"layui-form form\">" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\">标签名称</label>" +
        "<div class=\"layui-input-block\" style=\"width: 350px;\">" +
        "<select name=\"city\" class=\"hide_city\">" +
        "</select>" +
        "</div>" +
        "</div>" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\">分类名称</label>" +
        "<div class=\"layui-input-block\" style=\"width: 350px;\">" +
        "<input type=\"text\" name=\"hide_name\" placeholder=\"请输入分类名称\" class=\"layui-input hide_name\">" +
        "</div>" +
        "</div>" +
//        "<div class=\"layui-form-item\">" +
//        "<label class=\"layui-form-label\">分类排序</label>" +
//        "<div class=\"layui-input-inline\">" +
//        "<input type=\"text\" name=\"hide_password\" placeholder=\"请输入分类排序\" class=\"layui-input hide_password\" style=\"width: 350px;\">" +
//        "</div>" +
//        "</div>" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\">分类操作</label>" +
        "<div class=\"layui-input-inline\">" +
        "<button type=\"button\" class=\"layui-btn hide_save\" style=\"margin-left: 15px;\">保存</button>" +
        "</div>" +
        "</div>" +
        "</form>";





</script>